var ZYRequestClient = require("../../../utils/zy-request");
var requestClient;
var socketTask;
const app = getApp();
Page({
  data:{
    hasAdmin:false,
    hasApproval:false,
    permission:{
      sealList:false,
      addSeal:false,
      replaceSeal:false,
      electronicFence:false,
      alarmRecord:false,
      approvalProcess:false
    },
    functionList:[],
    navbarData:{
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '工作台', //导航栏 中间的标题
      white:true
     },
     // 导航头的高度
     height: app.globalData.height * 2 + 20
  },
  onLoad() {
    requestClient = new ZYRequestClient();
  },
  onShow(){
    var _this = this;
    _this.setData({
      hasAdmin:false
    });
    var userInfo = wx.getStorageSync('userInfo');
    if(userInfo==''){
      wx.showModal({
        title:'授权登录',
        content:'请授权登录使用更多功能！',
        showCancel:false,
        success(r){
          wx.navigateTo({
            url: '../../login/index',
          })
        }
      })
      return;
    } else {
      // 创建关闭websocket
      socketTask = requestClient.connectSocket();
      socketTask.onOpen(function(){
        // 连接成功获取审批状态
        var data = {
          'userId':userInfo.id,
          'operateType':'wait_approval'
        };
        socketTask.send({
          data:JSON.stringify(data)
        });
      });
      // 数据监听
      socketTask.onMessage(function(response){
        var messageData = JSON.parse(response.data);
        if(messageData.operateType == "wait_approval"){
          _this.setData({
            hasApproval:messageData.hasApproval
          });
          if(messageData.hasApproval){
            wx.showTabBarRedDot({
              index: 2,
              text: ''
            })
          } else {
            wx.removeTabBarBadge({
              index:2
            });
          }
        }
      });
      requestClient.getUserDefaultCompany(userInfo.id,function(res){
        if(res.data.data == null){
            wx.showToast({
              title: '请完善企业信息！',
              icon:'error',
              mask:true
            })
            setTimeout(() => {
              wx.switchTab({
                url: '../contacts/index',
              })
            }, 1000);
            return;
        } else {
            // 判断如果是超管则默认为true
            if(userInfo.id == res.data.data.belongUser){
              _this.setData({
                hasAdmin:true
              });
            } else {
              // 判断是否有管理员权限
              var functionList = res.data.data.adminFunctionList;
              for(var i = 0; i < functionList.length; i++){
                var fun = functionList[i];
                if(fun.code == 3008){
                  _this.setData({
                    hasAdmin:fun.enableFlag
                  });
                  break;
                }
              }
            }
            _this.setData({
              functionList:res.data.data.adminFunctionList
            });
            // 超级管理员拥有所有功能
            if(_this.data.hasAdmin){
              _this.setData({
                'permission.sealList':true,
                'permission.addSeal':true,
                'permission.replaceSeal':true,
                'permission.electronicFence':true,
                'permission.alarmRecord':true,
                'permission.approvalProcess':true,
                'permission.statistics':true,
              });
            } else {
              _this.setData({
                'permission.sealList':_this.getPermissionByCode(1001),
                'permission.addSeal':_this.getPermissionByCode(1002),
                'permission.replaceSeal':_this.getPermissionByCode(1004),
                'permission.electronicFence':_this.getPermissionByCode(1005),
                'permission.alarmRecord':_this.getPermissionByCode(1006),
                'permission.approvalProcess':_this.getPermissionByCode(2001),
                'permission.statistics':_this.getPermissionByCode(4001),
              });
            }
        }
      });
    }
  },
  onHide(){
    // 关闭websocket
    if(socketTask){
      socketTask.close();
      socketTask = null;
    }
  },
  // 下拉刷新
  onPullDownRefresh(e){
    this.onShow();
    wx.stopPullDownRefresh();
  },
  // 页面跳转
  bindFunctionClick(e){
    var url = e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
  /**
   * 根据功能代码获取权限
   * @param {Integer} code 
   */
  getPermissionByCode(code){
      var _this = this;
      var functionList = _this.data.functionList;
      for(var i = 0; i < functionList.length; i++){
        var fun = functionList[i];
        if(code == fun.code){
          return fun.enableFlag;
        } else {
          continue;
        }
      }

      return false;
  }
})